package com.guigu.www.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.www.pojo.*;
import com.guigu.www.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class JjController {
    @Autowired
    private GyServce gyServce;
    @Autowired
    private LxServce lxServce;
    @Autowired
    private GzjlService gzjlService;
    @Autowired
    private JybjService jybjService;
    @Autowired
    private ZyjnService zyjnService;
    @Autowired
    private FmService fmService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private XmfmService xmfmService;
    @RequestMapping("xm")
    public String xm(Model model,int id){
        Fm fm = fmService.queryAll();
        Xmfm xmfm = xmfmService.queryByid(id);
        model.addAttribute("fm",fm);
        model.addAttribute("xmfm",xmfm);
        return "blog-single";
    }
    //登录页面
    @RequestMapping("login")
    public String login(Model model,HttpSession session, Login l){
        Login login = loginService.loginUser(l);
        if(login!=null) {
            session.setAttribute("login",login);
            return "redirect:/houtai";
        }
            model.addAttribute("error", "登录失败");
            return "login";
    }
    //注销账号
    @RequestMapping("zhuxiao")
    public String zhuxiao(Model model,HttpSession session){
        session.removeAttribute("login");
        return "login";
    }
    //后台页面
    @RequestMapping("querygrxxwh")
    public String querygrxxwh(Model model){
        Gy gy = gyServce.queryAll();
        model.addAttribute("gy",gy);
        return "houtai";
    }
    //封面维护
    @RequestMapping("fmwh")
    public String fmwh(Model model){
        Fm fmwh = fmService.queryAll();
        model.addAttribute("fmwh",fmwh);
        return "houtai";
    }
    //专业技能维护
    @RequestMapping("zyjnwh")
    public String zyjnwh(Model model){
        String name=null;
        List<Zyjn> zyjnwh = zyjnService.queryAll(name);
        model.addAttribute("zyjnwh",zyjnwh);
        return "houtai";
    }
    //根基编号查询专业技能
    @RequestMapping("queryByzyid")
    public String queryByzyid(Model model,int id){
        Zyjn zyjn = zyjnService.queryByzyid(id);
        String name=null;
        List<Zyjn> zyjnwh = zyjnService.queryAll(name);
        model.addAttribute("zyjnwh",zyjnwh);
        model.addAttribute("zyjn",zyjn);
        return "houtai";
    }
    //删除专业技能
    @RequestMapping("delzyjn")
    public String delzyjn(Model model,int id){
        zyjnService.del(id);
        return "redirect:/houtai";
    }
    //添加专业技能
    @RequestMapping("tjzyjn")
    public String tjzyjn(Model model,Zyjn zyjn){
        zyjnService.tj(zyjn);
        return "redirect:/houtai";
    }
    //模糊查询专业技能
    @RequestMapping("mhcx")
    public String mhcx(Model model,String cx){
        List<Zyjn> zyjnwh = zyjnService.queryAll(cx);
        model.addAttribute("zyjnwh",zyjnwh);
        return "houtai";
    }
    //教育背景
    @RequestMapping("jybjwh")
    public String jybjwh(Model model){
        List<Jybj> jybjs = jybjService.queryAll();
        model.addAttribute("jybjs",jybjs);
        return "houtai";
    }
    //根据编号查询教育背景
    @RequestMapping("queryByjybjid")
    public String queryByjybjid(Model model,int id){
        Jybj jybjxg = jybjService.querybyjybjid(id);
        List<Jybj> jybjs = jybjService.queryAll();
        model.addAttribute("jybjs",jybjs);
        model.addAttribute("jybjxg",jybjxg);
        return "houtai";
    }
    //修改教育背景
    @RequestMapping("xgjybj")
    public String xgjybj(Model model,Jybj jybj){
        jybjService.xg(jybj);
        return "redirect:/houtai";
    }
    //添加
    @RequestMapping("tjjybj")
    public String tjjybj(Model model,Jybj jybj){
        jybjService.tj(jybj);
        return "redirect:/houtai";
    }
    //联系方式维护
    @RequestMapping("lxfswh")
    public String lxfswh(Model model){
        Lx lx = lxServce.queryAll();
        model.addAttribute("lxwh",lx);
        return "houtai";
    }
    //项目维护
    @RequestMapping("mxwh")
    public String xmwh(Model model,String xmm, HttpServletRequest request,
                       @RequestParam( value = "pageindex",defaultValue = "1") Integer pageindex,
                       @RequestParam(value = "pageSum",defaultValue = "1")Integer pageSum){
        if(pageindex<1){
            pageindex=1;
        }
        if(pageSum!=1){
            if(pageindex>pageSum){
                pageindex=pageSum;
            }
        }
        PageInfo<Xmfm> feye = xmfmService.feye(pageindex,xmm);
        model.addAttribute("xmwh",feye.getList());
        model.addAttribute("index",feye.getPageNum());
        model.addAttribute("PageSum",feye.getPages());
        return "houtai";
    }
    //根据项目编号查询
    @RequestMapping("queryByxmid")
    public String queryByxmid(Model model,String xmm,int id){
        Xmfm xgxmfm = xmfmService.queryByid(id);
        PageInfo<Xmfm> feye = xmfmService.feye(1,xmm);
        model.addAttribute("xmwh",feye.getList());
        model.addAttribute("index",feye.getPageNum());
        model.addAttribute("PageSum",feye.getPages());
        model.addAttribute("xgxmfm",xgxmfm);
        return "houtai";
    }
    @RequestMapping("queryByxmxqid")
    public String queryByxmxqid(Model model,String xmm,int id){
        Xmfm xqxmfm = xmfmService.queryByid(id);
        PageInfo<Xmfm> feye = xmfmService.feye(1,xmm);
        model.addAttribute("xmwh",feye.getList());
        model.addAttribute("index",feye.getPageNum());
        model.addAttribute("PageSum",feye.getPages());
        model.addAttribute("xqxmfm",xqxmfm);
        return "houtai";
    }
    //修改项目封面
    @RequestMapping("xgxmfm")
    public String xgxmfm(Model model,Xmfm xmfm){
        xmfmService.xg(xmfm);
        return "redirect:/houtai";
    }
    //添加
    @RequestMapping("tjxm")
    public String tjxm(Model model,Xmfm xmfm){
        xmfmService.tj(xmfm);
        return "redirect:/houtai";
    }
    //工作经历
    @RequestMapping("gzjlwh")
    public String gzjlwh(Model model){
        List<Gzjl> gzjls = gzjlService.queryAll();
        model.addAttribute("gzjls",gzjls);
        return "houtai";
    }
    @RequestMapping("tjgzjl")
    public String tjgzjl(Model model,Gzjl gzjl){
        gzjlService.tj(gzjl);
        return "redirect:/houtai";
    }
    @RequestMapping("queryBygzjlid")
    public String queryBygzjlid(Model model,int id){
        Gzjl xggzjl = gzjlService.querygzjlid(id);
        List<Gzjl> gzjls = gzjlService.queryAll();
        model.addAttribute("gzjls",gzjls);
        model.addAttribute("xggzjl",xggzjl);
        return "houtai";
    }
    //前端页面的内容
    @RequestMapping("queryAll")
    public String queryAll(Model model){
        Fm fm = fmService.queryAll();
        Gy gy = gyServce.queryAll();
        Lx lx = lxServce.queryAll();
        List<Jybj> jybj = jybjService.queryAll();
        String name=null;
        List<Zyjn> zyjns = zyjnService.queryAll(name);
        List<Gzjl> gzjls = gzjlService.queryAll();
        List<Xmfm> xmfms = xmfmService.queryAll();
        model.addAttribute("gy",gy);
        model.addAttribute("lx",lx);
        model.addAttribute("jybj",jybj);
        model.addAttribute("zyjns",zyjns);
        model.addAttribute("gzjls",gzjls);
        model.addAttribute("fm",fm);
        model.addAttribute("xmfms",xmfms);
        return "index";
    }
    //后台页面的内容
    @RequestMapping("houtai")
    public String houtai(Model model){
        Gy gy = gyServce.queryAll();
        model.addAttribute("gy",gy);
        return "houtai";
    }
    @RequestMapping("guanyu")
    public String Guanyu(Model model,Gy gy){
        gyServce.xg(gy);
        return "redirect:/houtai";
    }
    @RequestMapping("gzjl")
    public String Gzjl(Model model,Gzjl gzjl){
        gzjlService.xg(gzjl);
        return "redirect:/houtai";
    }
    @RequestMapping("xgzyjn")
        public String Zyjn(Model model,Zyjn zyjn){
        zyjnService.xg(zyjn);
        return "redirect:/houtai";
    }
    @RequestMapping("lx")
    public String Lx(Model model,Lx lx){
        lxServce.xg(lx);
        return "redirect:/houtai";
    }
    @RequestMapping("fm")
    public String Fm(Model model, Fm fm, MultipartFile file, HttpServletRequest request) throws IOException {
        String realPath = request.getServletContext().getRealPath("/images/resource");
        System.out.println(file);
        if (file.getSize()==0){
            System.out.println(111);
            System.out.println(fm);
            fmService.xg(fm);
        }else {
            //获取随机名称
            String s = UUID.randomUUID().toString();
            //获取文件后缀Z
            String filename = file.getOriginalFilename();
            System.out.println(filename);
            filename=filename.substring(filename.lastIndexOf("."));
            System.out.println(filename);
            //realPath是指文件上传的具体地址
            //file.getOriginalFilename() 文件的名称
            //文件上传
            String s2 = s + filename;
            file.transferTo(new File(realPath+"/"+s2));
            //获取文件名
            fm.setDximg(s2);
            System.out.println(fm);
            fmService.xg(fm);
        }
        return "redirect:/houtai";
    }
}
